package com.zcm.modules.cxf.interceptor;

import com.zcm.common.config.Constant;
import com.zcm.common.mapper.JsonMapper;
import com.zcm.common.service.BaseService;
import com.zcm.common.utils.SpringContextHolder;
import com.zcm.modules.cxf.entity.BabaResponse;
import com.zcm.modules.cxf.service.CxfSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BabaInterceptor extends BaseService implements HandlerInterceptor {

    @Autowired
    private CxfSessionService cxfSessionService;
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String sessionId = httpServletRequest.getHeader(Constant.SESSION_ID);
        if(cxfSessionService.getBySessionId(sessionId)==null){
            httpServletResponse.getOutputStream().write(JsonMapper.getInstance().toJson(new BabaResponse(10,0L,"login error",null)).getBytes());
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
